package cn.tedu.ivos2.dict.service.impl;

import cn.tedu.ivos2.dict.mapper.DictMapper;
import cn.tedu.ivos2.dict.pojo.dto.DictQuery;
import cn.tedu.ivos2.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos2.dict.pojo.entity.Dict;
import cn.tedu.ivos2.dict.pojo.vo.DictVO;
import cn.tedu.ivos2.dict.service.DictService;
import cn.tedu.ivos2.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> selectDict(DictQuery dictQuery) {
        log.debug("查询字典业务：dictQuery={}", dictQuery);
        List<DictVO> list = dictMapper.selectDict(dictQuery);
        return list;
    }

    @Override
    public void saveDict(DictSaveParam dictSaveParam) {
        log.debug("保存字典业务：dictSaveParam={}", dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam,dict);
        if (dict.getId()==null){
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }else {
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }

    @Override
    public void deleteDict(Long id) {
        log.debug("删除字典业务：id={}", id);
        dictMapper.delete(id);
        dictOptionMapper.deleteByDictId(id);
    }
}
